package mju.cxf.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import mju.cxf.constant.MessageConstant;
import mju.cxf.dto.ProductSpecificationsDTO;
import mju.cxf.entity.ProductSpecifications;
import mju.cxf.result.Result;
import mju.cxf.service.ProductSpecificationsService;
import mju.cxf.vo.ProductSpecificationsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "商品分类接口列表")
@RequestMapping("/admin/api/specifications")
public class ProductSpecificationsController {
    @Autowired
    private ProductSpecificationsService productSpecificationsService;

    /**
     * 新增商品信息
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新建商品信息")
    public Result<String> createProduct(@RequestBody ProductSpecificationsDTO productSpecificationsDTO) {

        return productSpecificationsService.saveProductSpecifications(productSpecificationsDTO) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 修改商品信息
     *
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改商品信息")
    public Result<String> updateProduct(@RequestBody ProductSpecificationsDTO productSpecificationsDTO) {
        return productSpecificationsService.updateProduct(productSpecificationsDTO) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 删除商品信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品信息", notes = "管理员通过此接口删除商品信息")
    public Result<String> deleteProduct(@PathVariable Integer id) {
        return productSpecificationsService.deleteProduct(id) ?
                Result.success(MessageConstant.SUCCESS_MESSAGE) :
                Result.error(MessageConstant.FAILURE_MESSAGE);
    }

    /**
     * 分页查询商品信息
     * @return 分页查询结果
     */
    @GetMapping("/pages")
    public Page<ProductSpecificationsVO> getUsersByUsername(@RequestParam(value = "name",required = false) String name,
                                                            @RequestParam(value = "state",required = false) Integer state,
                                                            @RequestParam(value = "minPrice",required = false) Integer minPrice,
                                                            @RequestParam(value = "maxPrice",required = false) Integer maxPrice,
                                                            @RequestParam(value = "minNumber",required = false) Integer minNumber,
                                                            @RequestParam(value = "maxNumber",required = false) Integer maxNumber,
                                                            @RequestParam(value = "current",defaultValue = "1") int current,
                                                            @RequestParam(value = "size",defaultValue = "10") int size) {
        return productSpecificationsService.selectPage(name, state, minPrice, maxPrice, minNumber, maxNumber, current, size);
    }


}
